﻿using System.Collections.Generic;
using com.tdb.foundation;
using com.tdb.posion.protocol.protobuf;
using UnityEngine;
using UnityEngine.Events;

public class MessageManager
{
    private Dictionary<Cmds, UnityAction<Message>> _handlerDic = new Dictionary<Cmds, UnityAction<Message>>();
    
    /// <summary>
    /// 处理消息
    /// </summary>
    public void HandlerMessage(Message msg)
    {
        Cmds cmd = msg.Cmds;
        if (_handlerDic.ContainsKey(cmd))
        {
            _handlerDic[cmd].Invoke(msg);
        }
        else
        {
            Debug.LogError("Not Handler Cmds " + cmd);
        }
    }
    /// <summary>
    /// 注册处理器
    /// </summary>
    /// <param name="cmds">需要响应的命令号</param>
    /// <param name="handler">对应的处理器</param>
    public void RegisterHandler(Cmds cmds, UnityAction<Message> handler)
    {
        _handlerDic.AddOrReplace(cmds, handler);
    }
    /// <summary>
    /// 需要解注的注册器
    /// </summary>
    /// <param name="cmds">对应的命令号</param>
    public void UnRegister(Cmds cmds)
    {
        _handlerDic.Remove(cmds);
    }

}
